package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.PermissionService;
import com.itheima.contant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    PermissionService permissionService;


    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {

        return permissionService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
    }
    @RequestMapping("/addPermission")
    public Result addPermission(@RequestBody Permission permission) {
        try {
            permissionService.addPermission(permission);
            return Result.success(MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.ADD_PERMISSION_FAIL);
        }

    }

    @RequestMapping("/deletePermission")
    public Result deletePermission(Integer id) {
        try {
            permissionService.deletePermission(id);
            return Result.success(MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }
    @RequestMapping("/queryPermissionById")
    public Result queryPermissionById(Integer id) {
        try {
          Permission permission = permissionService.queryPermissionById(id);
            return Result.success("", permission);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }


    }
    @RequestMapping("/updatePermission")
    public Result updatePermission(@RequestBody Permission permission) {
        try {
            permissionService.updatePermission(permission);
            return Result.success(MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.EDIT_PERMISSION_FAIL);
        }

    }
}
